﻿Public Class UCCapTaiKhoan
    Sub ddlNhanVienload()
        Dim sql As String = "select n.manv from nhanvien n where n.manv not in (select t.manv from taikhoan t)"
        ddlNhanVien.DataSource = DataProvider.LoadTable(sql)
        ddlNhanVien.ValueMember = "MANV"
        ddlNhanVien.DisplayMember = "MANV"
    End Sub
    Sub GridView_Load()
        Dim sql As String = "select * from taikhoan"
        GControl.DataSource = DataProvider.LoadTable(sql)
    End Sub

    Function TestInfo() As Boolean
        If ddlNhanVien.Text = "" Or txtTenDN.Text = "" Then
            ThongBao.TestInfo()
            Return False
        End If
        Return True
    End Function

    Private Sub UCCapTaiKhoan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ddlNhanVienload()
        GridView_Load
    End Sub
    Function KiemTraTaiKhoan() As Boolean
        Dim sql As String = String.Format("select * from taikhoan where username = '{0}'", txtTenDN.Text)
        Dim dt As DataTable = DataProvider.LoadTable(sql)
        If dt.Rows.Count <> 0 Then
            MessageBox.Show("Username đã tồn tại", "Thông báo")
            Return False
        End If
        Return True
    End Function
    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If Not TestInfo() Then
            Return
        End If
        If Not KiemTraTaiKhoan Then
            Return
        End If
        Dim maNV As Integer = Convert.ToInt16(ddlNhanVien.Text)
        Dim sql As String = String.Format("insert into TAIKHOAN(USERNAME,PASSWORD,MANV) values ('{0}','',{1})", txtTenDN.Text, maNV)
        DataProvider.ExecuteNonQuery(sql)
        GridView_Load()
        ddlNhanVienload
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Not ThongBao.Agree Then
            Return
        End If
        Dim manv As String = GView.GetFocusedRowCellDisplayText("MANV")
        Dim sql As String = String.Format("delete TAIKHOAN where MaNV = '{0}'", manv)
        DataProvider.ExecuteNonQuery(sql)
        GridView_Load()
        ddlNhanVienload
    End Sub
    Sub LoadThongTinMaNV()
        Dim drv As DataRowView = ddlNhanVien.SelectedItem
        Dim dr As DataRow = drv.Row
        Dim manv = dr("MANV")
        'If manv Is Nothing Then
        '    Return
        'End If
        Dim sql As String = String.Format("select * from nhanvien where manv = '{0}'", manv)
        Dim dt As DataTable = DataProvider.LoadTable(sql)
        GridControl1.DataSource = dt
        groupc.Text = "Thông tin mã nhân viên " & ddlNhanVien.Text
    End Sub
    Private Sub ddlNhanVien_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlNhanVien.SelectedIndexChanged
        LoadThongTinMaNV
    End Sub

    Private Sub GView_RowCellClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GView.RowCellClick
        Dim manv As String = GView.GetFocusedRowCellDisplayText("MANV")
        Dim sql As String = String.Format("select * from nhanvien where manv = '{0}'", manv)
        Dim dt As DataTable = DataProvider.LoadTable(sql)
        GridControl1.DataSource = dt
        groupc.Text = "Thông tin mã nhân viên " & manv
    End Sub
End Class
